<?php

namespace app\student\controller;

use app\student\model\CourseModel;

class Course extends \think\Controller
{

    //-查询科目
    public function selectCourse()
    {
        $selectCourse = CourseModel::selectCourseModel();
        $this->assign('selectCourse',$selectCourse);

        return $this->fetch('course/selectCourse');
    }

    //增加科目
    public function insertCourse()
    {
        if($this->request->isPost())
        {
            $insertCourse = $this->request->post();
            $result = CourseModel::insertCourseModel($insertCourse);
            if($result)
            {
                //不能随便使用 $this->fetch(); 这相当于对视图进行渲染，而如果视图有其它页面的变量时候，在进行渲染的话，就会报错

                return $this->success('增加成功','course/selectCourse','','1');
            }else{
                return $this->error('增加失败','course/selectCourse');
            }

        }else{
            return $this->fetch('course/insertCourse');
        }

    }

    //删除科目
    public function deleteCourse()
    {
        if($this->request->isGet())
        {
            $deleteCourse = $this->request->get();
            $result = CourseModel::deleteCourseModel($deleteCourse);
            //var_dump($result);die;
            if($result)
            {
                return $this->success('删除成功','course/selectCourse','','1');
            }else{
                return $this->error('删除失败','course/selectCourse','','1');
            }

        }else{
            return $this->error('删除失败','course/selectCourse','','1');
        }
    }

    //修改科目
    public function updateCourse()
    {
        if($this->request->isPost())
        {
            $updateCourse = $this->request->post();
            $result = CourseModel::updateCourse($updateCourse);
            if($result)
            {
                return $this->success('修改成功','course/selectCourse','','1');
            }else{
                $result = $this->request->post();
                $this->assign('result',$result);
                return $this->fetch('course/updateCourse');
            }

        }else{
            $get = $this->request->get();
            $result = CourseModel::GetCourseModel($get);
            if($result)
            {
                $this->assign('result',$result);
                return $this->fetch('course/updateCourse');
            }else{
                return $this->error('修改失败','course/selectCourse','','1');
            }


        }

    }

}